package com.cxs.client.impl;

import com.alibaba.fastjson.JSON;
import com.cxs.client.OutInterfaceClient;
import com.cxs.client.req.SendMsgReq;
import com.cxs.client.resp.AccessTokenResp;
import com.cxs.client.resp.BaiduWeatherResp;
import com.cxs.client.resp.CaiHongPiResp;
import com.cxs.config.CommonConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

/**
 * @Project: wechat-push
 * @Author: cxs2014501@163.com
 * @Create: 2023/9/7 10:40
 * @Description:
 **/
@Slf4j
@Service
public class OutInterfaceClientImpl implements OutInterfaceClient {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private CommonConfig commonConfig;

    @Override
    public BaiduWeatherResp getWeatherInfo() {
        String baiduWeatherUrl = commonConfig.getBaiduWeatherUrl();
        String url = baiduWeatherUrl
                .replace("[1]", commonConfig.getBaiduDistrictId())
                .replace("[2]", commonConfig.getBaiduAk());
        return restTemplate.getForObject(url, BaiduWeatherResp.class);
    }

    @Override
    public CaiHongPiResp getCaiHongPiInfo() {
        String tianxingCaihongpiUrl = commonConfig.getTianxingCaihongpiUrl();
        String url = tianxingCaihongpiUrl.replace("[1]", commonConfig.getTianxingKey());
        return restTemplate.getForObject(url, CaiHongPiResp.class);
    }

    @Override
    public AccessTokenResp getWeChatAccessToken() {
        String tokenUrl = commonConfig.getTokenUrl();
        String url = tokenUrl
                .replace("[1]", commonConfig.getAppId())
                .replace("[2]", commonConfig.getSecret());
        return restTemplate.getForObject(url, AccessTokenResp.class);
    }
}
